import {Component, OnInit, ViewChild} from '@angular/core';
import {adSystemApiUrl} from '../../../../config/api';
import {HttpService} from '../../../lib/service/http.service';
import {ModalDirective} from 'ngx-bootstrap';

@Component({
  selector: 'app-add-classify',
  templateUrl: 'add-classify.component.html',
  styleUrls: ['add-classify.component.scss']
})
export class AddClassifyComponent implements OnInit {

  type: string;
  message: string;
  isShow: boolean = false;

  totalItems: number;
  currentPage: number = 1;
  itemsPerPage: number = 12;

  firLevel: any = '';  // 一级类目列表
  firId: number;  // 选中的一级类目ID
  classifyName: string; // 分类名称
  remark: string;  // 备注
  classifyId: number;  // 分类ID
  searchData: string;  // 搜索内容

  @ViewChild('editModal') public editModal: ModalDirective;
  @ViewChild('delModal') public delModal: ModalDirective;

  constructor(private httpService: HttpService) {
  }

  ngOnInit() {
    this.getCategoryList(1)
  }

  //获取类目列表
  getCategoryList(page: number, name?: string) {
    if (!!name) {
      var url = `${adSystemApiUrl}/v1/admin/goods_category/list?level=0&parent=0&name=${name}&pageSize=${this.itemsPerPage}&page=${page}`;
    } else {
      var url = `${adSystemApiUrl}/v1/admin/goods_category/list?level=0&parent=0&pageSize=${this.itemsPerPage}&page=${page}`;
    }
    return this.httpService.httpGetAuth(url).subscribe(
      res => {
        this.totalItems = res.data.total;
        this.currentPage = res.data.current_page;
        this.firLevel = res.data.data;
      }
    )
  }

  // 搜索
  search() {
    this.getCategoryList(1, this.searchData)
  }

  // 重置
  reset() {
    this.searchData = null;
    this.getCategoryList(1);
  }

  // 添加弹框
  addClassify(id: number) {
    this.firId = id;
    this.classifyId = null;
    this.classifyName = null;
    this.remark = null;
    this.editModal.show();
  }

  // 编辑弹框
  editClassify(id: number, classifyId: number, classifyName: string, remark: string) {
    this.firId = id;
    this.classifyId = classifyId;
    this.classifyName = classifyName;
    this.remark = remark;
    this.editModal.show();
  }

  // 删除弹框
  delClassify(id: number) {
    this.classifyId = id;
    this.delModal.show();
  }

  // 确定删除
  delClassifySubmit() {
    const url = `${adSystemApiUrl}/v1/admin/goods_attribute_category/${this.classifyId}/del`;
    return this.httpService.httpPostAuth(url, '')
      .subscribe(
        res => {
          if (res.code === 2000) {
            this.type = 'success';
            this.message = '删除分类成功';
            this.isShow = true;
            this.classifyName = null;
            this.delModal.hide();
            this.getCategoryList(1)
          } else {
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  // 编辑分类提交
  addClassifySubmit() {
    if (!this.classifyName) {
      this.type = 'info';
      this.message = '请输入分类名称';
      this.isShow = true;
      return;
    }
    // 若classifyId存在，则为编辑，否则添加
    if (!!this.classifyId) {
      const url = `${adSystemApiUrl}/v1/admin/goods_attribute_category/edit`;
      let data = {name: this.classifyName, goods_category_id: this.firId, id: this.classifyId, remark: this.remark};
      return this.httpService.httpPostAuth(url, data)
        .subscribe(
          res => {
            if (res.code === 2000) {
              this.type = 'success';
              this.message = '修改分类成功';
              this.isShow = true;
              this.classifyName = null;
              this.editModal.hide();
              this.getCategoryList(1)
            } else {
              this.type = 'error';
              this.message = res.message;
              this.isShow = true;
            }
          }
        )
    } else {
      const url = `${adSystemApiUrl}/v1/admin/goods_attribute_category/add`;
      let data = {name: this.classifyName, goods_category_id: this.firId, remark: this.remark};
      return this.httpService.httpPostAuth(url, data)
        .subscribe(
          res => {
            if (res.code === 2000) {
              this.type = 'success';
              this.message = '添加类目成功';
              this.isShow = true;
              this.classifyName = null;
              this.editModal.hide();
              this.getCategoryList(1)
            } else {
              this.type = 'error';
              this.message = res.message;
              this.isShow = true;
            }
          }
        )
    }
  }

  promptIsShow(event: any): void {
    this.isShow = event;
  }

  pageChanged(event: any): void {
    window.scrollTo(0, 0);
    this.getCategoryList(event.page);
  }
}
